<?php
declare (strict_types = 1);

namespace app\controller;

use app\model\City;
use app\model\CityList;
use app\model\LikeCityList;
use app\model\User;
use think\facade\Filesystem;
use think\Request;

class CityListController
{

    protected $cityList;
    protected $likeCityList;
    protected $user;
    protected $city;
    public function __construct(){
        $this->cityList = new CityList();
        $this->likeCityList = new LikeCityList();
        $this->user = new User();
        $this->city = new City();
    }

    public function uploadList(Request $request)
    {
        // 获取表单上传文件 new File();
        $file = $request->file('file1');
        $file2 = $request->file('file2');
        $title = $request->post('title');
        $city_detail = $request->post('city_detail');
        $bast_month = $request->post('bast_month');
        $bast_detail = $request->post('bast_detail');
        $email = $request->post('email');
        $city_id = $request->post('city_id');

        $user_id = $this->user->where('email', $email)->value('id');

        if (!is_dir('public/animal_list')) {
            mkdir('public/animal_list', 0755, true);
        }

        // 如果是单文件上传
        if ($file && $file2) {
            // 上传到本地服务器
            $path = 'animal_list';
            $savename = Filesystem::putFile($path, $file);
            $savename2 = Filesystem::putFile($path, $file2);
            //            将图片上传到数据库中

            try {
                $this->cityList->insert([
                    "title" => $title,
                    "city_id" =>$city_id,
                    "city_detail" => $city_detail,
                    "user_id" => $user_id,
                    "city_img" => "http://127.0.0.1:8000/".$savename,
                    "bast_month"=>$bast_month,
                    "bast_detail"=>$bast_detail,
                    "bast_detail_img"=>"http://127.0.0.1:8000/".$savename2,
                    "hot" => 0,
                ]);
            }catch (\Exception $e){
                return json([
                    'code' => 400,
                    'msg' => $e->getMessage()
                ]);
            }


            return json(['code' => 200, 'status' => '旅游列表上传成功！']);
        }
        return json(['code' => 400, 'msg' => '旅游列表上传失败！']);
    }

    //    只要访问到这个页面的话就调用这个接口，让其+1
    public function addTop(Request $request)
    {
        $id = $request->post('id');
        try {
//            拿到旧值
            $old_hot = $this->cityList->where('id', $id)->value("hot");
            $new_hot = $old_hot + 1;
            $this->cityList->where('id', $id)->update(['hot' => $new_hot]);
        }catch (\Exception $e){
            return json(['code' =>400, 'msg' => $e->getMessage()]);
        }
        return json(['code' => 200,"message" => "添加成功" ,"data" => $new_hot]);


    }
    //
    public function myList(Request $request)
    {
        $email = $request->param('email');
        try {
            $user_id = $this->user->where('email', $email)->value('id');
            $city_list_id = $this->cityList->where('user_id',$user_id)->column('id');
            $res=$this->cityList->whereIn('id',$city_list_id)->field(["id","title","city_id","city_img","city_detail","hot"])->select();
            for ($i = 0; $i < count($res); $i++) {
                $res[$i]['city_name'] = $this->city->where('area_id',$res[$i]['city_id'])->value('name');
            }
        }catch (\Exception $e){
            return json([
                "code" => 500,
                "message" => $e->getMessage()
            ]);
        }
        return json([
            "code" => 200,
            "message" => "success",
            "data" => $res
        ]);
    }

    // 增加喜欢的城市，列表
    public function likeList(Request $request)
    {
        $email = $request->param('email');
//        根据邮箱来判断id
        try {
            $user_id = $this->user->where('email', $email)->value('id');
            $like_city_list_id = $this->likeCityList->where('user_id',$user_id)->column('like_city_list_id');
            $res=$this->cityList->whereIn('id',$like_city_list_id)->field(["id","title","city_id","city_img","city_detail","hot"])->select();
        }catch (\Exception $e){
            return json([
                "code" => 500,
                "message" => $e->getMessage()
            ]);
        }
        return json([
            "code" => 200,
            "message" => "success",
            "data" => $res
        ]);
    }


//    这里是获取最佳月份的数据，传递的对象格式是
    public function baseLists()
    {
//        首先拿到bast_month的数据
        try {
            for ($i=1;$i<=12;$i++){
                $month["month".$i] = $this->cityList->where('bast_month',$i)->field(["id","title","city_id","city_img","city_detail","hot"])->order("hot","desc")->limit(3)->select();
                //        遍历对象，加入数据
                for ($j = 0; $j < count($month["month".$i]); $j++) {
                    $month["month".$i][$j]['city_name'] = $this->city->where('area_id',$month["month".$i][$j]['city_id'])->value('name');
                }
            }
        }catch (\Exception $e){
            return json([
                "code"=>500,
                "message"=>$e->getMessage()
            ]);
        }


        return json([
            "code"=>200,
            "message"=>"获取信息成功！",
            "data" => $month,
        ]);
    }
//    获取热度排行
    public function hotTop()
    {
        try{
           $res = $this->cityList->field(["id","title","city_id","hot"])->order("hot","desc")->limit(100)->select();
        }catch (\Exception $e){
            return json([
                "code"=>500,
                "message"=>$e->getMessage()
            ]);
        }

//        遍历对象，加入数据
        foreach ($res as $key => $item) {
            $res[$key]['top'] = $key + 1;
        }

        return json([
            "code"=>200,
            "data"=>$res,
            "message" => "获取信息成功！"
        ]);

    }


    
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //时间不够的话就先获取全部
        /*        只需要返回以下字段
            id : number,
            title:string,
            city_id:number,
            city_img:string,
            city_detail:string,
            hot:number*/
        try {
            $res=$this->cityList->field(["id","title","city_id","city_img","city_detail","hot"])->select();
//            拿到一条触碰式列表的id
        }catch (\Exception $e){
            return json([
                "code" => 400,
                "msg" => $e->getMessage()
            ]);
        }

        for ($i = 0; $i < count($res); $i++) {
            $res[$i]['city_name'] = $this->city->where('area_id',$res[$i]['city_id'])->value('name');
        }


        return json([
            "code" => 200,
            "msg" => "查询成功",
            "data" => $res
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read(Request $request,$id)
    {
//        拿到邮箱，判断邮箱的
        $email = $request->param('email');
        //拿到id,因为这里是具体详情，所以这里显示所有关于的列表项
        try {
            $user_id = $this->user->where('email', $email)->value('id');
            $like_city_list_id = $this->likeCityList->where('user_id',$user_id)->where('like_city_list_id',$id)->count();
            if ($like_city_list_id > 0) {
                $like= true;
            }else{
                $like = false;
            }
            $res = $this->cityList->where('id',$id)->find();
            $res["like"] = $like;
        }catch (\Exception $e){
            return json([
                "code" => 400,
                "msg" => $e->getMessage()
            ]);
        }
        return json([
            "code" => 200,
            "data" => $res,
            "message"=>"获取城市列表成功！",
        ]);

    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //删除一条数据
        try {
            $res = $this->cityList->where('id',$id)->delete();
        }catch (\Exception $e){
            return json([
                "code" => 400,
                "msg" => $e->getMessage()
            ]);
        }

        if ($res) {
            return json([
                "code" => 200,
                "message"=>"删除成功！"
            ]);
        }else{
            return json([
                "code" => 400,
                "message"=>"删除失败!"
            ]);
        }

    }
}
